package com.ymatou.iapi.sellerproduct.testcase;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.GetProductProcessingActivityCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AdminCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtActivity;
import com.ymttest.database.model.YmtProductsInActivity;
import com.ymttest.database.sqlwapper.YmtActivityWapper;
import com.ymttest.database.sqlwapper.YmtProductsInActivityWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_GetProductProcessingActivity {
	
	
	private static String activityName = EnvSetup.getData("AdminLoginUser");
	private static String activityPswd = EnvSetup.getData("AdminLoginPswd");
	private static String sellerName = EnvSetup.getData("productsellername");
	private static String sellerId = EnvSetup.getData("productsellerid");
	private static String activityStartId = EnvSetup.getData("productActivityStartId");
	
	
	//private static String activityNotStartId = EnvSetup.getData("productActivityNotStartId");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品的活动信息查询");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetProductProcessingActivityCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductProcessingActivity() {
		Logger.start(true, "活动中商品信息查询");
		try{
			//发布商品参加活动
			String productId = SellerProductCallService.addNewProductAndApproveActivity(Integer.parseInt(sellerId),
					sellerName,activityName,activityPswd,activityStartId,null);
			
			//获取商品活动信息
			GetProductProcessingActivityCall getProductProcessingActivityCall = new GetProductProcessingActivityCall();
			getProductProcessingActivityCall.setData("ProductId="+productId+"&SellerId="+sellerId);
			getProductProcessingActivityCall.callService();
			JSONObject responseBean = new JSONObject(getProductProcessingActivityCall.getReturnData());
			JSONObject productInActBean = responseBean.getJSONObject("Data").getJSONObject("ProductInActivity");
			
			//获取活动信息
			YmtActivityWapper yaw = new YmtActivityWapper();
			YmtActivity ya = yaw.getRecordByActivityId(Integer.parseInt(activityStartId));
			
			
			YmtProductsInActivityWapper ypaw = new YmtProductsInActivityWapper();
			YmtProductsInActivity ypa = ypaw.selectByProductId(productId);
			
			
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
			//检查结果
			Logger.verifyEquals(200,responseBean.getInt("Code"), "响应报文-返回码");
			
			Logger.verifyEquals(Integer.parseInt(sellerId),productInActBean.getInt("UserId"), "响应报文-UserId");
			Logger.verifyEquals(ya.getSname(),productInActBean.getString("ActivityName"), "响应报文-ActivityName");
			Logger.verifyEquals(format.format(ypa.getDbegintime()),productInActBean.getString("StartTime"), "响应报文-StartTime");
			Logger.verifyEquals(format.format(ypa.getDendtime()) ,productInActBean.getString("EndTime"), "响应报文-EndTime");
			Logger.verifyEquals(Integer.parseInt(activityStartId),productInActBean.getInt("ActivityId"), "响应报文-ActivityId");
			Logger.verifyEquals(productId,productInActBean.getString("ProductId"), "响应报文-ProductId");
			
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}

	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductStopInActivity() {
		Logger.start(true, "活动中商品结束促销的信息查询");
		try{
			//发布商品参加活动
			String productId = SellerProductCallService.addNewProductAndApproveActivity(Integer.parseInt(sellerId),
					sellerName,activityName,activityPswd,activityStartId,null);
			
			//结束活动中的商品销售
			YmtProductsInActivityWapper ymtProductsInActivityWapper = new YmtProductsInActivityWapper();	
			YmtProductsInActivity productsInActivity = ymtProductsInActivityWapper.selectByProductId(productId);
			int productInActivityId = productsInActivity.getIproductinactivityid();
			
			AdminCallService.closeProductInActivity(String.valueOf(productInActivityId));
			
			
			//获取商品活动信息
			GetProductProcessingActivityCall getProductProcessingActivityCall = new GetProductProcessingActivityCall();
			getProductProcessingActivityCall.setData("ProductId="+productId);
			getProductProcessingActivityCall.callService();
			JSONObject responseBean = new JSONObject(getProductProcessingActivityCall.getReturnData());
			Object productInActBean = responseBean.getJSONObject("Data").get("ProductInActivity");
			
			//检查结果
			Logger.verifyEquals(200,responseBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals("null",productInActBean.toString(),"响应报文-ProductInActivity");
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}

	}
	
	
	
	
	
	
}
